'##############
'Door Script
'##############

'Controls Doors

'(c) 2007-2009 Rüdiger Hülsmann
'(c) 2007-2009 Marcel Kuhnt

'Script Version: 1.1
'Omsi release: 1.0

'Needs:
'- elec

'Revision History:
'- Marcel Kuhnt		13.06.2009	Batterietrennschalter => elec_busbar_main
'- Marcel Kuhnt		21.06.2009	AI-Unterstützung hinzugefügt	
'- Rüdiger Hülsmann	24.10.2009	Ansteuerung des Kinderwagenwunsches modifiziert
'- Rüdiger Hülsmann	19.12.2009	Timer für Ausstiegstür jetzt Timegap-basiert, Bugfix: Tür schließt jetzt nur bei elec_busbar=1
'- RÜdiger Hülsmann	22.09.2010	Door light trigger value set to 0.1
'- RÜdiger Hülsmann	07.11.2010	Door speed based on reservoir pressure, air-operated doors made an option
'- Marcel Kuhnt		08.11.2010	AI procedure closing doors debugged
'- Rüdiger Hülsmann	21.11.2010	Front doors only operable when busbar on
'- Rüdiger Hülsmann	23.12.2010	External front door operation included, option: doors only operable with actual stop brake
'- Rüdiger Hülsmann	31.12.2010	Electric door function debugged
'- Rüdiger Hülsmann	05.01.2011	20h-switch
'- Rüdiger Hülsmann	07.01.2011	Backdoor manual switch sound debugged, 20h switch depending on bus type
'- Rüdiger Hülsmann	19.01.2011	Electric door support
'--------------------------------------------------------------------------------
{trigger:dach_sw}
	(L.L.elec_busbar_main)
	{if}
		(M.L.trg_dach)
	{endif}
	1 (S.L.cockpit_dach_sw)
	(T.L.ev_kippschalter_ein)
{end}

{trigger:dach_sw_off}
	0 (S.L.cockpit_dach_sw)
	(T.L.ev_kippschalter_aus)
{end}

{trigger:dach1_sw}
	(L.L.elec_busbar_main)
	{if}
		(M.L.trg_dach1)
	{endif}
	1 (S.L.cockpit_dach1_sw)
	(T.L.ev_kippschalter_ein)
{end}

{trigger:dach1_sw_off}
	0 (S.L.cockpit_dach1_sw)
	(T.L.ev_kippschalter_aus)
{end}

{trigger:dach_sw_external}
	(L.L.elec_busbar_main) (L.L.bremse_halte_sw) (C.L.door_needs_stopbrake) ! && &&
	(L.L.elec_busbar_main) (L.L.bremse_halte) (C.L.door_needs_stopbrake) && && ||
	{if}
		(M.L.trg_dach)
	{endif}
{end}

{macro:dach_Init}
	1000 random 500 / 1 - (C.L.dach_acc_var) * (C.L.dach_acc) + (S.L.dachAcc)

	1000 random 500 / 1 - (C.L.dach_maxspeed_var) * (C.L.dach_maxspeed) + (S.L.dachMaxSpeed)


	0 (S.L.dachRefl)
	
{end}

{macro:dach_Frame}

	(M.L.dach_calc)
	(M.L.dach_rot)
	(M.L.dach1_calc)
	(M.L.dach1_rot)




{macro:dach_calc}
		(L.L.dachTarget)
		{if}
			(L.L.dach) (F.L.dach_out_speed) (S.L.dachMaxSpeed)
		{else}
			(L.L.dach) (F.L.dach_in_speed) (S.L.dachMaxSpeed)
		{endif}

		(C.L.dach_acc) (S.L.dachAcc)

		(L.L.dachTarget) ! (L.L.dachSpeed) s0 abs 0.05 > (L.L.dach) 0 > || && 
		{if}
			l0 /-/ (L.L.dachMaxSpeed) <
			{if}
				(L.L.dachSpeed) (L.L.dachAcc) (L.S.Timegap) * - (S.L.dachSpeed)
			{else}
				(L.L.dachSpeed) (L.L.dachAcc) (L.S.Timegap) * + (S.L.dachSpeed)
			{endif}
			(L.L.dach) (L.L.dachSpeed) (L.S.Timegap) * + (S.L.dach)
		{else}
			(L.L.dachSpeed) abs 0.05 > (L.L.dach) 1 < || (L.L.dachTarget) &&
			{if}
				l0 (L.L.dachMaxSpeed) <
				{if}
					(L.L.dachSpeed) (L.L.dachAcc) (L.S.Timegap) * + (S.L.dachSpeed) 
				{else}
					(L.L.dachSpeed) (L.L.dachAcc) (L.S.Timegap) * - (S.L.dachSpeed) 
				{endif}
				(L.L.dach) (L.L.dachSpeed) (L.S.Timegap) * + (S.L.dach)
			{else}
				0 (S.L.dachSpeed)
			{endif}
		{endif}

	(L.L.dach) 1 >
	{if}
		(T.L.dach_out_hit)
		1 (S.L.dach)
	{else}
		(L.L.dach) 0 <
		{if}
			(T.L.dach_in_hit)
			0 (S.L.dach)
		{endif}
	{endif}
{end}

{macro:dach_rot}
	(L.L.kneel_allowed)
	{if}
		(L.L.dach) (F.L.dach_rot_angle_kneel) (S.L.dach_rot_angle)
	{else}
		(L.L.dach) (F.L.dach_rot_angle_no_kneel) (S.L.dach_rot_angle)
	{endif}
{end}


{macro:dach1_calc}
		(L.L.dach1Target)
		{if}
			(L.L.dach1) (F.L.dach1_out_speed) (S.L.dach1MaxSpeed)
		{else}
			(L.L.dach1) (F.L.dach1_in_speed) (S.L.dach1MaxSpeed)
		{endif}

		(C.L.dach1_acc) (S.L.dach1Acc)

		(L.L.dach1Target) ! (L.L.dach1Speed) s0 abs 0.05 > (L.L.dach1) 0 > || && 
		{if}
			l0 /-/ (L.L.dach1MaxSpeed) <
			{if}
				(L.L.dach1Speed) (L.L.dach1Acc) (L.S.Timegap) * - (S.L.dach1Speed)
			{else}
				(L.L.dach1Speed) (L.L.dach1Acc) (L.S.Timegap) * + (S.L.dach1Speed)
			{endif}
			(L.L.dach1) (L.L.dach1Speed) (L.S.Timegap) * + (S.L.dach1)
		{else}
			(L.L.dach1Speed) abs 0.05 > (L.L.dach1) 1 < || (L.L.dach1Target) &&
			{if}
				l0 (L.L.dach1MaxSpeed) <
				{if}
					(L.L.dach1Speed) (L.L.dach1Acc) (L.S.Timegap) * + (S.L.dach1Speed) 
				{else}
					(L.L.dach1Speed) (L.L.dach1Acc) (L.S.Timegap) * - (S.L.dach1Speed) 
				{endif}
				(L.L.dach1) (L.L.dach1Speed) (L.S.Timegap) * + (S.L.dach1)
			{else}
				0 (S.L.dach1Speed)
			{endif}
		{endif}

	(L.L.dach1) 1 >
	{if}
		(T.L.dach1_out_hit)
		1 (S.L.dach1)
	{else}
		(L.L.dach1) 0 <
		{if}
			(T.L.dach1_in_hit)
			0 (S.L.dach1)
		{endif}
	{endif}
{end}

{macro:dach1_rot}
	(L.L.kneel_allowed)
	{if}
		(L.L.dach1) (F.L.dach1_rot_angle_kneel) (S.L.dach1_rot_angle)
	{else}
		(L.L.dach1) (F.L.dach1_rot_angle_no_kneel) (S.L.dach1_rot_angle)
	{endif}
{end}

'---------------------------------------------------------------------
'---------------------------------------------------------------------
'                         --               --
'                        ----             ----
'                         --               --
'
'                                  -                                   
'                                  -                                   
'                                 --- 
'                           --           --
'                             --       --
'                               -------
'---------------------------------------------------------------------
'---------------------------------------------------------------------
{macro:trg_dach}

	(L.L.dachTarget) ! (S.L.dachTarget) 
	{if}
		(T.L.dach_roll_out)
	{else}
		(T.L.dach_roll_in)
	{endif}
{end}


{macro:trg_dach1}

	(L.L.dach1Target) ! (S.L.dach1Target) 
	{if}
		(T.L.dach_roll_out)
	{else}
		(T.L.dach_roll_in)
	{endif}
{end}